package com.woniuxy.ServletContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @auther linke
 * @create 2021-10-19 14:35
 */
@WebServlet("/servletContextDemo03")
public class ServletContextDemo03 extends HttpServlet {
    /**
     * 测试servletContext作用域
     * servletContext作用域是整个项目范围，服务开启时创建，服务关闭时消亡
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       /* //获取servletContext对象
        ServletContext servletContext = this.getServletContext();
        //设置属性
        servletContext.setAttribute("name","marx");*/

        System.out.println("servletContextDemo03...执行了");

       //设置属性
       req.getServletContext().setAttribute("name","marx");

    /*   //测试请求转发
        req.getRequestDispatcher("/servletContextDemo04").forward(req,resp);*/

        //测试重定向
        resp.sendRedirect("/web03/servletContextDemo04");


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
